package com.cnunicom.modules.tra.controller;

import com.cnunicom.common.utils.DateUtils;
import com.cnunicom.common.utils.R;
import com.cnunicom.modules.tra.entity.RoomTypeEntity;
import com.cnunicom.modules.tra.service.RoomTypeService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 房型表
 *
 * @author dzygod
 * @email dingziyuan0@gmail.com
 * @date 2020-01-06 11:06:17
 */
@RestController
@RequestMapping("tra/roomtype")
public class RoomTypeController {
    @Autowired
    private RoomTypeService roomTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("tra:roomtype:list")
    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = roomTypeService.queryPage(params);
        List<RoomTypeEntity> list = roomTypeService.list();
        return R.ok().put("list", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("tra:roomtype:info")
    public R info(@PathVariable("id") Integer id){
		RoomTypeEntity roomType = roomTypeService.getById(id);
        return R.ok().put("roomType", roomType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("tra:roomtype:save")
    public R save(@RequestBody RoomTypeEntity roomType){
        if (StringUtils.isBlank(roomType.getFloor())) {
            roomType.setType(1);
        } else {
            roomType.setParentId(0);
            roomType.setType(0);
        }
        String thisTime = DateUtils.getDateTime();
        roomType.setCreateTime(thisTime);
        roomType.setUpdateTime(thisTime);
        roomType.setIsDel(0);
		roomTypeService.save(roomType);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("tra:roomtype:update")
    public R update(@RequestBody RoomTypeEntity roomType){
        roomType.setUpdateTime(DateUtils.getDateTime());
		roomTypeService.updateById(roomType);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("tra:roomtype:delete")
    public R delete(@RequestBody Integer[] ids){
        roomTypeService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
